package com.topone.Utils.wx.builder.template.service;

import com.google.common.collect.ImmutableMap;

import static  com.topone.Const.Model.*;
import com.topone.Utils.wx.builder.template.WxTemplateMessageService.WxTemplateDataBuilder;
import com.topone.dataObject.BaseEntity;
import com.topone.dataObject.TcUser;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import  static com.topone.Utils.wx.builder.template.WxTemplateMessageService.create;
import  static com.topone.Utils.wx.builder.template.WxTemplateMessageService.throwInvalidArgumentObjectException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author eddie
 */
public class WxFanzoneTemplateMessageService {

    protected final static Logger LOG = LoggerFactory.getLogger(WxFanzoneTemplateMessageService.class);

    public static class UserUpdatePhoneSuccessTemplBuilder implements WxTemplateDataBuilder {

        @Override
        public WxMpTemplateMessage build(BaseEntity entity) {
            // XX，您好，您的手机号已经校验成功
            // 手机号：尾号3346
            // 校验结果：校验通过
            //  您的手机号已经校验通过，你可以前往网点领取货品。
            // ----------------------------
            if (entity instanceof TcUser) {
                TcUser tcUser = (TcUser) entity;

                String phone = StringUtils.isNotEmpty(tcUser.getHandPhone()) ?
                        tcUser.getHandPhone().replace(tcUser.getHandPhone().substring(3, 7), "****")
                        : "";
                List<WxMpTemplateData> data = Arrays.asList(
                        create("first", tcUser.getUserName() + ",您好，您正在修改手机号", "#57bfe9"),
                        create("keyword1", phone),
                        create("keyword2", "修改手机号码"),
                        create("remark", "您正在修改手机号码，如非本人操作，请立即联系我们", "#dd3f64"));
                return WxMpTemplateMessage
                        .builder()
                        .templateId("iBuBFCnYPVJTnxaGvgwvXR1zYgOT5spbVcmu0fYYSo0")
                        .data(data).build();
            }
            return throwInvalidArgumentObjectException(entity);
        }
    }

    // message for reminder
    private final Map<String, WxTemplateDataBuilder> MESSAGE_SUBMIT_LIST = ImmutableMap.<String, WxTemplateDataBuilder>builder()
            .put(UPDATE_PHONE, new UserUpdatePhoneSuccessTemplBuilder())
            .build();

    public WxTemplateDataBuilder getSubmitMessageBuilder(String messageType) {
        return MESSAGE_SUBMIT_LIST.get(messageType);
    }

}
